package com.example.video

import android.app.ActionBar.LayoutParams
import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import android.view.Gravity
import android.view.LayoutInflater
import com.google.android.material.snackbar.Snackbar
import androidx.appcompat.app.AppCompatActivity
import androidx.navigation.findNavController
import androidx.navigation.ui.AppBarConfiguration
import androidx.navigation.ui.navigateUp
import androidx.navigation.ui.setupActionBarWithNavController
import android.view.Menu
import android.view.MenuItem
import android.widget.Button
import android.widget.EditText
import android.widget.PopupWindow
import android.widget.TextView
import android.widget.Toast
import androidx.core.view.isVisible
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.blankj.utilcode.util.ToastUtils
import com.example.base.entity.ParamSend
import com.example.base.entity.ParamSendChild
import com.example.base.entity.Reply
import com.example.base.mvvm.BaseActivity
import com.example.video.adapter.ReplyAdapter
import com.example.video.adapter.ReplyChildAdapter
import com.example.video.databinding.ActivityMainBinding
import java.text.SimpleDateFormat

class MainActivity : BaseActivity<ActivityMainBinding,MainViewModel>() {
    lateinit var replyAdapter: ReplyAdapter
    var content = ""
    override fun initView() {
        replyAdapter = ReplyAdapter()
        binding.rv.adapter = replyAdapter
        binding.rv.layoutManager = LinearLayoutManager(this)

        viewModel.reply(0,"6954645983678431758")

        //点击评论，弹出popupwindow,实现发送评论
        binding.tvReply.setOnClickListener {
            val popupWindow =PopupWindow()
            //四大属性 --宽 高  内容  点击外部消失
            popupWindow.width = LayoutParams.MATCH_PARENT
            popupWindow.height = LayoutParams.WRAP_CONTENT
            val view = LayoutInflater.from(this).inflate(R.layout.item_pop, null)

            popupWindow.contentView = view
            popupWindow.isOutsideTouchable = true
            popupWindow.isFocusable = true//焦点

            view.findViewById<Button>(R.id.send).setOnClickListener {
                val paramSend = ParamSend()
                paramSend.nickname = "张三"
                paramSend.content = view.findViewById<EditText>(R.id.et).text.toString()
                paramSend.createtime = SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(System.currentTimeMillis())
                paramSend.itemid = 6954645983678431758
                viewModel.send(paramSend)
                view.findViewById<EditText>(R.id.et).setText("")
            }
            val editText = view.findViewById<EditText>(R.id.et)
            editText.addTextChangedListener(object:TextWatcher{
                override fun beforeTextChanged(
                    s: CharSequence?,
                    start: Int,
                    count: Int,
                    after: Int
                ) {

                }

                override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
                    content = s.toString()
                }

                override fun afterTextChanged(s: Editable?) {

                }
            })
            popupWindow.showAtLocation(binding.tvReply,Gravity.BOTTOM,0,0)
        }
        //点击展开，查看子评论，点击回复，发送子评论
        replyAdapter.addChildClickViewIds(R.id.tv_more,R.id.reply_toreply)
        replyAdapter.setOnItemChildClickListener { adapter, view, position ->
            when (view.id){
                R.id.tv_more->{
                    //找到子评论
                    val recyclerView =
                        replyAdapter.getViewByPosition(position, R.id.reply_rv) as RecyclerView
                    //将展开更多得到文字变成收起
                    val textView =
                        replyAdapter.getViewByPosition(position, R.id.tv_more) as TextView
                    //是否展示收起
                    if(recyclerView.isVisible){
                        recyclerView.isVisible = false
                        textView.text = "----展开更多----"
                    }else{
                        recyclerView.isVisible = true
                        textView.text = "收起评论"
                    }

                }
                R.id.reply_toreply->{
                    val paramSendChild = ParamSendChild()
                    paramSendChild.nickname = "张三"
                    paramSendChild.content = content
                    paramSendChild.commentid = replyAdapter.data[position].id
                    paramSendChild.createtime = SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(System.currentTimeMillis())
                    viewModel.sendChild(paramSendChild)
                    //刷新数据
                    //找到子评论的布局  //找到适配器 添加数据并刷新
                    val recyclerView =
                        replyAdapter.getViewByPosition(position, R.id.reply_rv) as RecyclerView
                    val replyChildAdapter = recyclerView.adapter as ReplyChildAdapter
                    replyChildAdapter.data.add(0,Reply(0,replyAdapter.data[position].id,content,SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(System.currentTimeMillis()),0,"张三",0,0,""))
                    replyChildAdapter.notifyDataSetChanged()
                }
            }
        }
    }

    override fun initData() {
        viewModel.replySuccess.observe(this){
            if(it.code==200){
                replyAdapter.data.clear()
                replyAdapter.data.addAll(it.data)
                replyAdapter.notifyDataSetChanged()
            }
        }
        viewModel.sendSuccess.observe(this){
            if(it.code==200){
                replyAdapter.data.add(0,it.data)
                replyAdapter.notifyDataSetChanged()
            }
        }
        viewModel.sendChildSuccess.observe(this){
            if(it.code==200){
                ToastUtils.showLong(it.msg)
            }
        }
    }

    override fun getViewModel(): Class<MainViewModel> {
        return MainViewModel::class.java
    }

    override fun getLayout(): Int {
        return R.layout.activity_main
    }


}